package de.clue.corpus.twitter.reader;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmoticonPolarityElementAppender implements IElementAppender {

    Pattern all =
            Pattern.compile
            ("[o>}\\])]? # hair, optional \n" +
             "[:;8] # eyes \n" +
             "[',]? # subeyes, optional \n" +
             "-? # nose, optional \n" +
             "(?: [sdpco\\#@*$|] " +
             " | \\)\\)? " +
             " | \\(\\(? ) # mouth \n" +
             "=? # beard, optional \n",
             Pattern.COMMENTS | Pattern.CASE_INSENSITIVE);
    
    Pattern positiv =
            Pattern.compile
            ("[o>}\\])]? # hair, optional \n" +
                    "[:;8x] # eyes \n" +
                    "[',]? # subeyes, optional \n" +
                    "-? # nose, optional \n" +
                    "(?: [sdpo\\#@*$|] " +
                    " | \\)\\)? ) # mouth \n" +
                    "=? # beard, optional \n",
                    Pattern.COMMENTS | Pattern.CASE_INSENSITIVE);
    
    Pattern negativ =
            Pattern.compile
            ("[o>}\\])]? # hair, optional \n" +
                    "[:;8] # eyes \n" +
                    "[',]? # subeyes, optional \n" +
                    "-? # nose, optional \n" +
                    "(?: [c\\#@*$|] " +
                    " | \\(\\(? ) # mouth \n" +
                    "=? # beard, optional \n",
                    Pattern.COMMENTS | Pattern.CASE_INSENSITIVE);
    
    
    @Override
    public void process(StringBuffer output, String message) {
        
        int positiveCount = 0;
        int negativeCount = 0;
        
        Matcher m = positiv.matcher(message);
        while (m.find()) {
            System.out.println("POSITIV: Found " + m.group() + " at " + m.start() + " to " + m.end());
            positiveCount++;
        }
        m = negativ.matcher(message);
        while (m.find()) {
            System.out.println("NEGATIV: Found " + m.group() + " at " + m.start() + " to " + m.end());
            negativeCount++;
        }    
        
        
        String polarity = "UNKNOWN";
        /*
        if(positiveCount > negativeCount){
            polarity = "POSITIVE";
        } else if (negativeCount > positiveCount){
            polarity = "NEGATIVE";
            
        }
        */
        if(positiveCount > 0 && negativeCount == 0){
            polarity = "POSITIVE";
            
        } else if (negativeCount > 0 && positiveCount == 0){
            polarity = "NEGATIVE";
            
        } else if(positiveCount > 0 && negativeCount > 0){
            polarity = "POISONED";
            
        }
        
        System.out.println("FINAL: " + polarity);
        
        output.append("<polarity>");
        output.append(polarity);
        output.append("</polarity>");
        

    }

}
